# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=wasmtime
pkgver=18.0.3
pkgrel=0
pkgdesc="Fast and secure runtime for WebAssembly"
url="https://wasmtime.dev/"
# others unsupported
arch="aarch64 x86_64"
license="Apache-2.0"
depends_dev="libwasmtime=$pkgver-r$pkgrel"
makedepends="
	cargo
	cargo-auditable
	chrpath
	rust-wasm
	zstd-dev
	"
subpackages="libwasmtime $pkgname-dev"
source="https://github.com/bytecodealliance/wasmtime/releases/download/v$pkgver/wasmtime-v$pkgver-src.tar.gz
	system-zstd.patch
	"
builddir="$srcdir/wasmtime-v$pkgver-src"
# net: fetch dependencies
# check: custom_limiter_detect_os_oom_failure fails with oom for some reason
options="!check net"


prepare() {
	default_prepare

	git init -q
	# can't patch deps with vendor dir
	rm -fv .cargo/config.toml
	rm -rf vendor

	# Rust target triple.
	local target=$(rustc -vV | sed -n 's/host: //p')

	# Build against system-provided libs
	mkdir -p .cargo
	cat >> .cargo/config.toml <<-EOF
		[target.$target]
		zstd = { rustc-link-lib = ["zstd"] }
	EOF

	cargo fetch --target="$CTARGET" --locked
}

build() {
	cargo auditable build --frozen --release
	cargo auditable build --frozen --release -p wasmtime-c-api
}

check() {
	cargo test --frozen
}

package() {
	install -Dm755 target/release/wasmtime -t "$pkgdir"/usr/bin/
	# remove gigantic useless rpath to /usr/lib
	chrpath -d target/release/libwasmtime.so
	install -Dm644 target/release/libwasmtime.so -t "$pkgdir"/usr/lib/
	cp -a crates/c-api/include "$pkgdir"/usr/
}

libwasmtime() {
	amove usr/lib
}

sha512sums="
de00e97c7075b6b0bf1dc7b1189512bd47cad60cae30d4885bedd070d2efabeb2fb5bf3409b49ad2713e2f8cc141186811bcb0527fc92cb4c9c278aea8713af5  wasmtime-v18.0.3-src.tar.gz
5441687022cee3a4774052cf81aef786bfb6385f0474c13964320c529b9f82128063f088c6a6e3e3f2883b592b118f13e0dacb309697d0e8d8e2d9670e33bba7  system-zstd.patch
"
